﻿#region ... Copyright Notice ...
/*
   Copyright 2008 Tyler Jensen

	Author: Tyler Jensen    http://www.tsjensen.com/blog

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
#endregion

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Atrax.Web.Test
{
	public partial class Callback : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			string alldata = string.Empty;
			using (StreamReader sr = new StreamReader(this.Request.InputStream))
			{
				alldata = sr.ReadToEnd();
			}

			//convert to strings
			string[] pairs = alldata.Split('&');
			NameValueCollection form = new NameValueCollection(pairs.Length);
			foreach (string pair in pairs)
			{
				string[] keyvalue = pair.Split('=');
				if (keyvalue.Length == 2)
				{
					form.Add(keyvalue[0], HttpUtility.UrlDecode(keyvalue[1]));
				}
			}

			if (alldata.Length > 0 && this.Request.HttpMethod.ToUpper() == "POST")
			{
				if (form["queryid"] != null
					&& form["clientqueryid"] != null
					&& form["querytype"] != null
					&& form["authcode"] != null
					&& form["statuscode"] != null
					&& form["statusdescription"] != null
					&& form["resultschema"] != null
					&& form["result"] != null)
				{
					string queryid = form["queryid"];
					string clientqueryid = form["clientqueryid"];
					string querytype = form["querytype"];
					string authcode = form["authcode"];
					string statuscode = form["statuscode"];
					string statusdescription = form["statusdescription"];
					string resultschema = form["resultschema"];
					string result = form["result"];

					StoreResult(queryid, clientqueryid, querytype, authcode, statuscode, statusdescription, resultschema, result);

					Response.Write("200 OK");
				}
				else
					Response.Write("501 Invalid data");
			}
			else
				Response.Write("599 GET method not supported.");

			Response.End();
		}

		private void StoreResult(string queryid, string clientqueryid, string querytype, string authcode, string statuscode, string statusdescription, string resultschema, string result)
		{
			//todo: do something smarter with results for testing
			//string homePath = System.Web.HttpContext.Current.Server.MapPath("~/");
			string dataDirectory = AppDomain.CurrentDomain.GetData("DataDirectory") as string; //a little hack to get the path to the App_Data folder
			string fileName = Path.Combine(dataDirectory, queryid + ".xml");
			File.WriteAllText(fileName, result);
		}
	}
}
